package api;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

import java.util.Set;
import java.util.concurrent.TimeUnit;

public class Browser {
    private WebDriver driver;

    public Browser(String url){
        System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
        driver.get(url);
        driver.manage().window().maximize();
    }

    public String getUrl(){
        return driver.getCurrentUrl();
    }

    public String getText(String cssPath){
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
        return driver.findElement(By.cssSelector(cssPath)).getText();
    }

    public String getValue(String cssPath,String attribute){
        return driver.findElement(By.cssSelector(cssPath)).getAttribute(attribute);
    }

    public void reflash(){
        driver.navigate().refresh();
    }

    public void goTo(String url){
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
        driver.get(url);
    }

    public void back(){
        driver.navigate().back();
    }

    public void switchWindow(){
        String handle = driver.getWindowHandle();
        Set<String> handles = driver.getWindowHandles();
        for (String str : handles){
            if (str.equals(handle)==false){
                driver.switchTo().window(str);
            }
        }
    }

    public void clickClassName(String cssPath){
        Actions action = new Actions(driver);
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
        action.click(driver.findElement(By.cssSelector(cssPath))).perform();
    }

    public void clickName(String name){
        Actions action = new Actions(driver);
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
        action.click(driver.findElement(By.name(name))).perform();
    }

    public void clickLink(String linkName){
        Actions action = new Actions(driver);
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
        action.click(driver.findElement(By.linkText(linkName))).perform();
    }

    public void input(String cssPath,String text){
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
        driver.findElement(By.cssSelector(cssPath)).sendKeys(text);
    }

    public void moveToAndStay(String cssPath, int millis){
        Actions action = new Actions(driver);
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
        action.moveToElement(driver.findElement(By.cssSelector(cssPath))).perform();
        this.stay(millis);
    }

    public boolean exist(String cssPath){
        try {
            driver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
            driver.findElement(By.cssSelector(cssPath));
        }catch (Exception e){
            return false;
        }
        return true;
    }

    public void stay(int millis){
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public void exit(){
        driver.quit();
    }


}
